home *** CD-ROM | disk | FTP | other *** search
/ Directorty Opus 5 - Magellan 2 / Opus 5 - Magellan 2.iso / Extras / NewIconsV4 / Developers / Source / ShowNI / showni.c < prev   
C/C++ Source or Header  |  1997-09-24  |  9KB  |  355 lines

  1. /* ShowNI 40.1 - By Eric Sauvageau */
  2.  
  3. /* This will take a list of icons, and display them in a window as 
  4.    buttons.  ClassAct is required.
  5.  
  6.    If the SELECT option is enabled, then clicking on an icon will output
  7.    the file's name (as passed on the commandline, so it might or might not 
  8.    have the ".info" suffix.  This is not a problem for CopyNewIcon, but
  9.    you must be aware of this if you intend to use ShowNI for other 
  10.    applications).  ShowNI will exist immediately in that case.
  11.  
  12.    Handy for icon selection in Installer scripts.
  13.    
  14.    First real program I write from scratch in C, so bear with me for 
  15.    any oddities.  I still prefer E :)
  16.  
  17.    Usage:
  18.  
  19.    SELECT\S   = Boolean.  If present, ShowNI exits at the first selected
  20.                 icon, returning the filename.
  21.  
  22.    TITLE\K    = Optional string for the window's titlebar.  Default is 
  23.                 "ShowNI".
  24.  
  25.    LABEL\K    = Optional string for the label shown while in Select mode.
  26.                 Default is "Select an icon:".
  27.  
  28.    ICONS\M\A  = A list of icons to show in the window.  A maximum of
  29.                 15 icons are supported.
  30.  
  31. */
  32.  
  33. /*** Uncomment to enable debug output to stdout ***/
  34.  
  35. /* #define DEBUG */
  36.  
  37.  
  38.  
  39. /*** Maximum icons supported ***/
  40.  
  41. #define MAXICONS 15
  42.  
  43.  
  44. #include <clib/macros.h>
  45. #include <clib/alib_protos.h>
  46. #include <libraries/gadtools.h>
  47. #include <intuition/icclass.h>
  48. #include <intuition/intuition.h>
  49.  
  50. #include <proto/exec.h>
  51. #include <proto/dos.h>
  52. #include <proto/utility.h>
  53. #include <proto/graphics.h>
  54. #include <proto/intuition.h>
  55. #include <proto/asl.h>
  56.  
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60.  
  61. #include <classact.h>
  62. #include <classact_author.h>
  63.  
  64. #include <proto/button.h>
  65. #include <proto/window.h>
  66. #include <proto/layout.h>
  67. #include <proto/label.h>
  68. #include <proto/newicon.h>
  69.  
  70. #include <gadgets/button.h>
  71. #include <classes/window.h>
  72.  
  73. #include <gadgets/layout.h>
  74. #include <images/label.h>
  75.  
  76. #include <libraries/newicon.h>
  77.  
  78.  
  79.  
  80.  
  81. /*** GadgetIDs ***/
  82. enum
  83. {
  84.    ID_LAYOUT=1,
  85.    ID_BUTROW,
  86.    ID_WINDOW,
  87.    ID_LABEL,
  88.    ID_BUT1,
  89.  
  90.    ID_LAST
  91. };
  92.  
  93.  
  94. enum
  95. {
  96.    ARG_LABEL=0,
  97.    ARG_TITLE,
  98.    ARG_SEL,
  99.    ARG_ICONS,
  100.    ARG_LAST
  101. };
  102.  
  103.  
  104.  
  105. /* Being global, the elements are initialized to 0. */
  106.  
  107. long ARG[ARG_LAST];
  108. struct Image *imagenorm[MAXICONS];
  109. struct Image *imagesel[MAXICONS];
  110. struct NewDiskObject *diskobjects[MAXICONS];
  111.  
  112.  
  113. struct NewIconBase  *NewIconBase;
  114.  
  115.  
  116. struct NewDiskObject *MyGetNewDiskObject(UBYTE *name);
  117. VOID mystrcpy(UBYTE *to,UBYTE *from);
  118.  
  119.  
  120. int main()
  121. {
  122.     struct RDArgs *args;
  123.    BOOL selectmode;
  124.    char **filenames;
  125.    BOOL HasValidIcons=FALSE;
  126.  
  127.    int count;
  128.  
  129.    struct Window *window;
  130.    Object *Objects[ID_LAST];
  131.  
  132.    struct Screen *scr;
  133.  
  134.     if (!(args = ReadArgs("LABEL/K,TITLE/K,S=SELECT/S,ICONS/M/A", ARG, NULL)))
  135.     {
  136.       PrintFault(IoErr(), NULL);    /* prints the appropriate err message */
  137.       return RETURN_ERROR;
  138.    }
  139.  
  140.    if (scr = LockPubScreen(NULL))
  141.    {
  142.  
  143.       NewIconBase = (struct NewIconBase *) OpenLibrary("newicon.library",39L);
  144.  
  145.       if (WindowBase && LayoutBase && ButtonBase && NewIconBase && LabelBase)
  146.       {
  147.  
  148. /*** Process passed arguments, opening icons and remapping them ***/
  149.  
  150.          selectmode = ARG[ARG_SEL];
  151. #ifdef DEBUG
  152.          if (selectmode) printf("Select mode\n");
  153. #endif
  154.  
  155.          count = 0;
  156.          filenames = (char **) ARG[ARG_ICONS];
  157.  
  158.          while ((*filenames) && (count < MAXICONS))
  159.          {
  160.  
  161.     /*** Using Nicola's version - it removes any trailing .info ***/
  162.  
  163.             if (diskobjects[count] = MyGetNewDiskObject(*filenames))
  164.             {
  165.  
  166. /* If we can get (at least one ) valid image, set HasValidIcons to TRUE. */
  167.  
  168.                if (imagenorm[count] = RemapChunkyImage(diskobjects[count]->ndo_NormalImage,scr)) (HasValidIcons = TRUE);
  169.                imagesel[count]  = RemapChunkyImage(diskobjects[count]->ndo_SelectedImage,scr);
  170.             }
  171. #ifdef DEBUG
  172.             printf("count =%d   imagenorm = %ld   imagesel = %ld   dobj = %ld\n",count, imagenorm[count], imagesel[count], diskobjects[count]);
  173.             printf("name = %s\n",(*filenames));
  174. #endif
  175.             count++;
  176.             filenames++;
  177.          };
  178.  
  179. /* Check if we have at least one valid NewIcon image to display */
  180.  
  181.          if (HasValidIcons)
  182.          {
  183.  
  184.             Objects[ID_WINDOW] = WindowObject, 
  185.                WA_Title, ((ARG[ARG_TITLE]) ? (char *)ARG[ARG_TITLE] : "ShowNI"),
  186.                WA_DepthGadget, TRUE,
  187.                WA_DragBar, TRUE,
  188.                WA_CloseGadget, TRUE,
  189.                WA_Activate,TRUE,
  190.                WINDOW_Position, WPOS_CENTERSCREEN,
  191.                WINDOW_ParentGroup, Objects[ID_LAYOUT] = VLayoutObject,
  192.                   LAYOUT_SpaceOuter, TRUE,
  193.                   LAYOUT_DeferLayout, TRUE,
  194.  
  195.                   LAYOUT_AddChild, Objects[ID_BUTROW] = HLayoutObject, HCentered,
  196.  
  197. /*** Empty group - we'll add stuff later. ***/
  198.  
  199.                   EndGroup,
  200.  
  201.                EndMember,
  202.             EndWindow;
  203.  
  204.             if (Objects[ID_WINDOW])
  205.             {
  206.  
  207. /* If select mode, then display the header label */
  208.                if (selectmode)
  209.                {
  210.                     SetAttrs(Objects[ID_LAYOUT],
  211.                        LAYOUT_AddImage, Objects[ID_LABEL] = LabelObject,
  212.                           IA_Font, scr->Font,
  213.                           LABEL_Text, ( ARG[ARG_LABEL] ? (char *)ARG[ARG_LABEL] : "Select an icon:"),
  214.                        EndImage,
  215.                   TAG_DONE);
  216.                }
  217.  
  218.                count = 0;
  219.                filenames = (char **) ARG[ARG_ICONS];
  220.  
  221.                while ((*filenames)  && (count < MAXICONS))
  222.                {
  223.  
  224. /*** Only add it if we do have an icon ***/
  225.  
  226.                   if (imagenorm[count])
  227.  
  228.                         SetAttrs(Objects[ID_BUTROW],
  229.                             LAYOUT_AddChild, ButtonObject,
  230.                                 GA_ID, (ID_BUT1+count),
  231.                                 GA_RelVerify, TRUE,
  232.                                  GA_Image, imagenorm[count],
  233.                                  GA_SelectRender, imagesel[count],
  234.                              ButtonEnd,
  235.                         CHILD_WeightedWidth,0,
  236.                         CHILD_WeightedHeight,0,
  237.                          TAG_DONE);
  238.  
  239.                      count++;
  240.                      filenames++;
  241.  
  242.                }
  243.  
  244.  
  245.                if (window = (struct Window *) CA_OpenWindow(Objects[ID_WINDOW]))
  246.  
  247. /*** We'll need it again below ***/
  248.  
  249.                filenames = (char **) ARG[ARG_ICONS];
  250.  
  251.                {
  252.                   ULONG wait, signal, result, done = FALSE;
  253.                   WORD code;
  254.  
  255.                   GetAttr(WINDOW_SigMask, Objects[ID_WINDOW], &signal);
  256.  
  257.                   while (!done)
  258.                   {
  259.                      wait = Wait(signal|SIGBREAKF_CTRL_C);
  260.  
  261.                      if (wait & SIGBREAKF_CTRL_C) done = TRUE;
  262.                      else
  263.                
  264.                      while ((result = CA_HandleInput(Objects[ID_WINDOW], &code)) != WMHI_LASTMSG)
  265.                      {
  266.                         switch (result & WMHI_CLASSMASK)
  267.                         {
  268.                            case WMHI_CLOSEWINDOW:
  269.                               done = TRUE;
  270.                               break;
  271.                      
  272.                            case WMHI_GADGETUP:
  273.                               if (selectmode)
  274.                               {
  275.                                  printf("%s\n", filenames[ (LONG) (result & WMHI_GADGETMASK) - ID_BUT1]);
  276.                                  done = TRUE;
  277.                               }
  278.                               break;
  279.  
  280.                         }
  281.                      }
  282.                   }
  283.                }
  284.  
  285.                DisposeObject(Objects[ID_WINDOW]);
  286.             }
  287.          }
  288.          else
  289.          {
  290.             printf("No valid NewIcon specified.\n");
  291.          }
  292.       }
  293.  
  294.  
  295. /*** Freeing our images and icons ***/
  296.  
  297.       count=0;
  298.  
  299. /***
  300.    Filename ptr was restored just after opening our GUI, so
  301.    no need to restore it again.
  302. ***/
  303.  
  304.       while ((*filenames)  && (count < MAXICONS))
  305.       {
  306. #ifdef DEBUG
  307.          printf("count =%d   imagenorm = %ld   imaegsel = %ld   dobj = %ld\n",count, imagenorm[count], imagesel[count], diskobjects[count]);
  308. #endif
  309.          if (imagenorm[count]) FreeRemappedImage(imagenorm[count],scr);
  310.          if (imagesel[count])  FreeRemappedImage(imagesel[count],scr);
  311.          if (diskobjects[count])   FreeNewDiskObject(diskobjects[count]);
  312.          count++;
  313.          filenames++;
  314.       }
  315.  
  316.       if (scr)  UnlockPubScreen(NULL,scr);
  317.  
  318.    }
  319.  
  320. /*** We're closing down. ***/
  321.  
  322.    if (NewIconBase) CloseLibrary((struct Library *) NewIconBase);
  323.  
  324.     FreeArgs(args);
  325.  
  326. }
  327.  
  328.  
  329.  
  330. /*** read an icon stripping trailing .info from name ***/
  331.  
  332. struct NewDiskObject *MyGetNewDiskObject(UBYTE *name)
  333. {
  334.    UBYTE buf[128];
  335.  
  336.  
  337.    mystrcpy(buf,name);
  338.    return(GetNewDiskObject(buf));
  339. }
  340.  
  341.  
  342. VOID mystrcpy(UBYTE *to,UBYTE *from)
  343. {
  344.    LONG len;
  345.  
  346.  
  347.    strcpy(to,from);
  348.    len = strlen(to);
  349.    if (len > 5 && !stricmp(&to[len-5],".info"))
  350.        to[len-5] = 0;
  351. }
  352.  
  353.  
  354. UBYTE versionstring20[] = "\0$VER: ShowNI 40.1 (8.7.97)";
  355.